package com.abc.controller;

import com.abc.entity.Product;
import com.abc.entity.User;
import com.abc.service.UserService;
import com.abc.utils.ValidateImageCodeUtils;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.omg.PortableInterceptor.USER_EXCEPTION;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.jws.WebParam;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    // 登录方法
    @PostMapping("/login")
    public String login(String username, String password, String loginType, HttpSession session) {

        if (loginType == null) loginType = "visitor"; // 默认游客登陆

        User login = userService.login(username, password); // 数据库查询
        if (login == null) return "redirect:/index"; // 跳转回到登录
        session.setAttribute("user", login); // session中存入用户信息

        if ("admin".equals(loginType) && "admin".equals(login.getUserType())) { // 管理员登陆
            System.out.println("管理员登陆成功!");
            return "redirect:/product/findAll?type=admin"; // 跳转到查询所有
        } else if ("user".equals(loginType) && "user".equals(login.getUserType())) { // 用户登陆
            System.out.println("普通用户登录成功");
            return "redirect:/product/findAll?type=user"; // 跳转到查询所有
        } else if ("visitor".equals(loginType) && "visitor".equals(login.getUserType())) {
            return "redirect:/product/findAll?type=visitor";
        } else {
            return "redirect:/index";
        }
    }

    // 注册方法
    @PostMapping("/register")
    public String register(User user, String code, HttpSession session) {
        userService.register(user); // 注册
        System.out.println("注册成功");
        return "redirect:/index"; // 注册成功跳转到登录界面
    }

    // 查询所有数据
    @GetMapping("findAll")
    public String findAll(Model model) {
        List<User> users = userService.findAll();
        model.addAttribute("users", users);
        return "/ims/admin/userlist";
    }

    // 查询单个用户信息
    @GetMapping("findUser")
    public String findUser(String id, Model model) {
        User user = userService.find(id);
        System.out.println(user);
        model.addAttribute("user", user);
        return "/ims/user/updateUser";
    }

    // 批准游客成为注册用户
    @GetMapping("approve")
    public String approve(String id) {
        User user = userService.find(id);
        user.setUserType("user"); // 注册用户
        userService.update(user);
        return "redirect:/user/findAll";
    }

    // 将注册用户取消为游客
    @GetMapping("disapprove")
    public String disapprove(String id) {
        User user = userService.find(id);
        user.setUserType("visitor"); // 注册用户
        userService.update(user);
        return "redirect:/user/findAll";
    }

    // 更新产品信息
    @PostMapping("/update")
    public String update(User user) {
        userService.update(user);
        return "redirect:/user/findUser?id=" + user.getId();
    }

}
